<template>
  <el-dialog
    :title="!dataForm.forId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="申请类型" prop="forType">
      <el-input v-model="dataForm.forType" placeholder="申请类型"></el-input>
    </el-form-item>
    <el-form-item label="申请原因" prop="forCause">
      <el-input v-model="dataForm.forCause" placeholder="申请原因"></el-input>
    </el-form-item>
    <el-form-item label="退款金额" prop="refund">
      <el-input v-model="dataForm.refund" placeholder="退款金额"></el-input>
    </el-form-item>
    <el-form-item label="申请说明" prop="forExplain">
      <el-input v-model="dataForm.forExplain" placeholder="申请说明"></el-input>
    </el-form-item>
    <el-form-item label="申请售后图片" prop="aftersaleImg">
      <el-input v-model="dataForm.aftersaleImg" placeholder="申请售后图片"></el-input>
    </el-form-item>
    <el-form-item label="申请时间" prop="forDate">
      <el-input v-model="dataForm.forDate" placeholder="申请时间"></el-input>
    </el-form-item>
    <el-form-item label="订单号" prop="orderNum">
      <el-input v-model="dataForm.orderNum" placeholder="订单号"></el-input>
    </el-form-item>
    <el-form-item label="售后同意 1、同意  2、不同意" prop="state">
      <el-input v-model="dataForm.state" placeholder="售后同意 1、同意  2、不同意"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          forId: 0,
          forType: '',
          forCause: '',
          refund: '',
          forExplain: '',
          aftersaleImg: '',
          forDate: '',
          orderNum: '',
          state: ''
        },
        dataRule: {
          forType: [
            { required: true, message: '申请类型不能为空', trigger: 'blur' }
          ],
          forCause: [
            { required: true, message: '申请原因不能为空', trigger: 'blur' }
          ],
          refund: [
            { required: true, message: '退款金额不能为空', trigger: 'blur' }
          ],
          forExplain: [
            { required: true, message: '申请说明不能为空', trigger: 'blur' }
          ],
          aftersaleImg: [
            { required: true, message: '申请售后图片不能为空', trigger: 'blur' }
          ],
          forDate: [
            { required: true, message: '申请时间不能为空', trigger: 'blur' }
          ],
          orderNum: [
            { required: true, message: '订单号不能为空', trigger: 'blur' }
          ],
          state: [
            { required: true, message: '售后同意 1、同意  2、不同意不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.forId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.forId) {
            this.$http({
              url: this.$http.adornUrl(`/patient/foraftersale/info/${this.dataForm.forId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.forType = data.forAftersale.forType
                this.dataForm.forCause = data.forAftersale.forCause
                this.dataForm.refund = data.forAftersale.refund
                this.dataForm.forExplain = data.forAftersale.forExplain
                this.dataForm.aftersaleImg = data.forAftersale.aftersaleImg
                this.dataForm.forDate = data.forAftersale.forDate
                this.dataForm.orderNum = data.forAftersale.orderNum
                this.dataForm.state = data.forAftersale.state
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/patient/foraftersale/${!this.dataForm.forId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'forId': this.dataForm.forId || undefined,
                'forType': this.dataForm.forType,
                'forCause': this.dataForm.forCause,
                'refund': this.dataForm.refund,
                'forExplain': this.dataForm.forExplain,
                'aftersaleImg': this.dataForm.aftersaleImg,
                'forDate': this.dataForm.forDate,
                'orderNum': this.dataForm.orderNum,
                'state': this.dataForm.state
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
